import os
import re

def search_image_usages(root_directory, image_filename):
    image_usages = []
    pattern = r'\[UIImage\s+imageNamed\s*?:\s*?@"{}"\]'.format(re.escape(image_filename))
    
    for root, dirs, files in os.walk(root_directory):
        for file in files:
            file_path = os.path.join(root, file)
            if file_path.endswith(('.m', '.mm', '.swift', '.xib', '.storyboard')):
                with open(file_path, 'r', encoding='utf-8') as f:
                    content = f.read()
                    if re.search(pattern, content):
                        image_usages.append(file_path)
    return image_usages

def main():
    root_directory = input("请输入项目根目录: ")  # 输入项目根目录
    image_filename = input("请输入要查找的图片文件名: ")  # 输入要查找的图片文件名
    
    if not os.path.isdir(root_directory):
        print("错误：指定的项目根目录不存在或不是一个目录！")
        return
    
    image_usages = search_image_usages(root_directory, image_filename)
    
    if not image_usages:
        print("在项目中没有发现使用该图片的文件。")
    else:
        print("在以下文件中发现了使用该图片的内容：")
        for file_path in image_usages:
            print(file_path)

if __name__ == "__main__":
    main()
